package su2010.puz.impl;

import java.awt.image.BufferedImage;

import su2010.puz.Filter;
import su2010.puz.util.PixelExtractor;

/**
 * Filter that differences an image with its inverted self.
 * It is posible to go in left-right or up-down direction.
 * @author Bruno
 *
 */
public class SymmetrifyFilter implements Filter {

	// true: left-right
	// false: up-down
	private boolean direction = true;
	
	public SymmetrifyFilter(boolean direction ){
		this.direction = direction;
	}
	public SymmetrifyFilter(){}
	
	@Override
	public BufferedImage apply(BufferedImage image) {
		int height = image.getHeight();
		int width = image.getWidth();
		int[] pixels = new int[height*width];		
		int[] pixelsDiff = new int[height*width];
		PixelExtractor.getRGB(image, 0, 0, width, height, pixels);
		
		for(int i = 0; i<width; i++)
			for(int j=0; j<height; j++){	
				if(direction){
					pixelsDiff[i+j*width] = (pixels[i+j*width]-pixels[(j+1)*width-i-1]);
					if(pixelsDiff[i+j*width]<0) pixelsDiff[i+j*width]=-pixelsDiff[i+j*width];
				}else{
					pixelsDiff[i+j*width] = (pixels[i+j*width]-pixels[(height-j-1)*width+i]);
					if(pixelsDiff[i+j*width]<0) pixelsDiff[i+j*width]=-pixelsDiff[i+j*width];
				}
			}
		
		BufferedImage out = new BufferedImage(width, height,image.getType());
		PixelExtractor.setRGB(out, 0, 0, width, height, pixelsDiff);
		return out;
	}

}
